package com.allianz.webapps.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appAddRiders.AddRiders;
import com.allianz.webapps.https.interfaces.appAddRiders.pojo.AddRidersBody;
import com.allianz.webapps.orderspojo.App;
import com.fasterxml.jackson.databind.ObjectMapper;

//http://localhost:8080/pad_marketing/main/addRiders.action
@Controller
@RequestMapping(value="main")
public class AddRidersController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AddRiders ards;
	/**
	 * 添加附加险
	 */
	@RequestMapping(value="addRiders")
	@ResponseBody
	public App addRiders(@RequestParam(value="jsonParam") String jsonParam, HttpServletRequest request,HttpServletResponse response)
			throws Exception{
		AddRidersBody arb=objectMapper.readValue(jsonParam, AddRidersBody.class);
		String body=ards.requestLoginParam(arb);
		logger.info(body);
		JSONObject json=new JSONObject(body);
		App app=null;
		String body1="";
		if (json.get("Body").equals(null)) {
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1=json.getString("Body");
			app=objectMapper.readValue(body1, App.class);
		}
		return app;
	}
}
